package com.example.chartview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.example.chartview.bean.ChartEntity

/**
 * 版权：浩博智能科技有限公司 版权所有
 * @author kevin
 * 创建日期：2021/7/2
 * 描述： 三根柱子 的柱状图
 *
 */
class MyChartThreeView(context: Context, attrs: AttributeSet?) : View(context, attrs) {


    private var maxValue:Int = 0

    // 绘制 X 轴 Y 轴
    private var mPaintX: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    // 绘制 柱子的
    private var mPaintRect: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    //  绘制 X 轴 Y 轴文字
    private var mPaintTextNonius : Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var rect:RectF ?=null

    private var lineWidth = 2f
    private var maxWid = 0
    private var maxHeight = 0

    private var linePaddingTop = 40f   // XY轴线的居 顶部 间隔
    private var linePaddingStart = 80f   // XY轴线的居 左 间隔
    private var linePaddingEnd = 40f   // 内容的居 右 间隔
    private var linePaddingBottom = 0f   // XY轴线的居 底部 间隔

    private val xTextLength = 3 //x 轴显示的数量
    private val yTextLength = 5 //y 轴显示的数量

    private val rectWidth  =  80  // 柱状图柱子的宽度

    private var list = mutableListOf<ChartEntity>()      // 柱子图的数据（ 底部的文字、柱子长度  ）




    //初始化画笔
    init {
        initPaint()
    }

    private fun initPaint() {

        mPaintX?.apply {
            color = resources.getColor(R.color.lineColor)  //画笔颜色
            strokeWidth = lineWidth    //画笔宽度
            style = Paint.Style.FILL    //画笔的样式，粗体 还是空心STROKE 还是填充 FILL_AND_STROKE
        }

        mPaintRect?.apply {
            color = Color.BLUE  //画笔颜色
            strokeWidth = lineWidth    //画笔宽度
            style = Paint.Style.FILL_AND_STROKE    //画笔的样式，粗体 还是空心STROKE 还是填充 FILL_AND_STROKE
        }

        mPaintTextNonius.color =resources.getColor(R.color.textColor)
        mPaintTextNonius.textSize = 30f
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //获取当前View的宽高
        maxWid = w
        maxHeight = h
        //渐变
//        mShader = LinearGradient(mViewWidth.toFloat(), mViewHeight.toFloat(), mViewWidth.toFloat(), 0f, intArrayOf( Color.TRANSPARENT, resources.getColor(R.color.blue)), null, Shader.TileMode.REPEAT)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
//
        drawLineNonius(canvas!!) //
        drawAxisLine(canvas!!)
        drawRect(canvas!!)


    }

    /*
    * 画柱子
    * */
    private fun drawRect(canvas: Canvas) {
        rect = RectF(100f,100f,105f,200f)
        var rectMaxHei = maxHeight - linePaddingBottom
        val gridWid = (maxWid - linePaddingStart -linePaddingEnd)/list.size

        var startR = (gridWid-rectWidth)/2


        for (index in list.indices) {
            var value = list[index].size

            var ks =  gridWid*index
            var left = linePaddingStart + startR +ks
            var top = rectMaxHei - rectMaxHei/(index+1)
            var right  = linePaddingStart+startR+ rectWidth +ks
            var bottom  = rectMaxHei

            mPaintRect.shader =LinearGradient(
                    0F,
                    0F,
                    0f ,
                    rectMaxHei  ,
                    Color.parseColor("#FF1372F0"),
                    Color.parseColor("#001372F0"),
                    Shader.TileMode.MIRROR
            )
            Log.d("gridWidindexmax ",rectMaxHei.toString())
            Log.d("gridWidindex", top.toString())



            canvas.drawRoundRect(left, top + linePaddingTop,right,bottom-2,2f,2f,mPaintRect)

        }



    /*    rect = Rect(100,100,105,200)    //画柱子可以
        canvas!!.drawRect(rect!!,mPaintX)*/

    }

    /*
    * 画游标码 xy 轴 （数字/文字） 等
   * */
    private fun drawLineNonius(canvas: Canvas) {
//      先计算每格的宽度
        val gridWid = (maxWid - linePaddingStart -linePaddingEnd)/xTextLength
        var str = "1"
        var tvHei  = getTextHeight(str,mPaintTextNonius)
        var tvWid  = getTextWidth(str,mPaintTextNonius) +2 // 加2显得更自然一些
        /*
        *  线居底部的距离
        * */
        linePaddingBottom = tvHei +20f+20f
//      每格子的高度
        val gridHei = (maxHeight - linePaddingBottom- linePaddingTop)/yTextLength
        /*
        *  绘制 X 轴上 （数字/文字）
        * */
        for(i in 0..xTextLength){
            if(i == xTextLength){
                break
            }
            var str = i.toString()
            var tvWid  = getTextWidth(str,mPaintTextNonius)
            var xTextIndex = (gridWid - tvWid )/2
            var zs = linePaddingStart+xTextIndex+(i*gridWid)
            canvas.drawText(str,linePaddingStart+xTextIndex+(i*gridWid),maxHeight.toFloat() -20f ,mPaintTextNonius)
        }


        /*
        *  绘制 Y 轴上 （数字/文字）
        * */
        for(i in 0..yTextLength){
            var str = i.toString()

//             不绘制 0 刻度的文字
            var yTextIndex =linePaddingBottom - (tvHei/2)
            canvas.drawText(str, (linePaddingStart-tvWid)/2, maxHeight - yTextIndex - (i * gridHei) ,mPaintTextNonius)

        }

    }

    /*
    *
    * 绘制 xy 轴线条
    * */
    private val drawAxisLine = { canvas:Canvas ->

        /*
         * 画横线 X轴线
         * */
        canvas!!.drawLine(linePaddingStart,maxHeight-linePaddingBottom,maxWid.toFloat(),maxHeight-linePaddingBottom,mPaintX)
        /*
        * 画竖线 Y轴线
        * */
        canvas!!.drawLine(linePaddingStart,linePaddingTop,linePaddingStart,maxHeight-linePaddingBottom,mPaintX)

    }


    fun setList(listData: MutableList<ChartEntity>, max: Int) {
        this.list = listData
        this.maxValue = max
        invalidate()
    }


    /*
    *  获取文字宽度
    * */
    private fun getTextWidth(text:String, paint:Paint ):Int {
        var rect = Rect() // 文字所在区域的矩形
        paint.getTextBounds(text, 0, text.length, rect)
        return rect.width()
    }

    /*
    *  获取文字高度
    * */
    private fun getTextHeight(text:String, paint:Paint ):Int {
        var rect = Rect() // 文字所在区域的矩形
        paint.getTextBounds(text, 0, text.length, rect)
        return rect.height()
    }

}